/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.awt.Font;

/**
 *
 * @author PLT - BGDL group
 */
public class gamePanel extends JPanel{

    BufferedImage buffer;
    boardPanel parent;
    GridLayout layout;
    
    Board board;
    
    double width;
    double height;
    double cellWidth;
    double cellHeight;
    
    public gamePanel() {
        this.setIgnoreRepaint(true);
        this.setFocusable(true);
    }
    
    public void Initialize() {
        buffer = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
        
        parent = (boardPanel) this.getParent();
        board = parent.game.board;
        
        width = parent.getSize().width-parent.iPanel.getSize().width;
        height = parent.getSize().height;
        cellWidth = Math.round(width/board.getWidth());
        cellHeight = Math.round(height)/board.getHeight();
    }

    public void drawBuffer() throws InterruptedException {
        Graphics2D b = buffer.createGraphics();
        b.setColor(Color.black);
        b.fillRect(0, 0, 800, 600);

        b.setColor(Color.green);
        
        //draw lines in leftmost and rightmost
        b.drawLine((int)cellWidth, 0, (int)cellWidth, (int)height);
        b.drawLine((int)cellWidth*(board.getWidth()-1), 0, (int)cellWidth*(board.getWidth()-1), (int)height);
        
        //draw lines in upmost and lowest
        b.drawLine(0, (int)cellHeight, (int)width, (int)cellHeight);
        b.drawLine(0, (int)cellHeight*(board.getHeight()-1), (int)width, (int)cellHeight*(board.getHeight()-1));
        
        //draw middle line in upper and lower cells
        for(int i = 2;i<=(board.getWidth()-2);i++){
        	//upper
        	b.drawLine((int)cellWidth*i, 0, (int)cellWidth*i, (int)cellHeight);
        	//lower
        	b.drawLine((int)cellWidth*i, (int)cellHeight*(board.getHeight()-1) , (int)cellWidth*i, (int)height);
        }
        //draw middle line in left and right cells
        for(int i = 2;i<=(board.getHeight()-2);i++){
        	//left
            b.drawLine(0, (int)cellHeight*i, (int)cellWidth, (int)cellHeight*i);
        	//right
        	b.drawLine((int)cellWidth*(board.getWidth()-1), (int)cellHeight*(i) , (int)width, (int)(int)cellHeight*(i));
        }
        
        drawPlayers(b);
        
        b.dispose();
    }
    
    public void drawPlayers(Graphics2D b) {
      Player[] pList = board.getPlayer();
      Font font = new Font("SansSerif", Font.PLAIN, 10);
      b.setFont(font);
      
      HashMap<String, ArrayList<String> > drawMap = new HashMap<String, ArrayList<String>>();
      String loc, name;
      Player p;
      ArrayList<String> a;
      for(int i=0; i<pList.length; i++) {
        p = pList[i];
        loc = p.getLocationX() + "," + p.getLocationY();
        a = drawMap.get(loc);
        if(a == null) {
          a = new ArrayList<String>();
        }
        a.add(p.getName());
        drawMap.put(loc, a);
      }
      
      Set<String> keySet = drawMap.keySet();
      Iterator<String> iter = keySet.iterator();
      String key;
      int x, y;
      ArrayList<String> val;
      while(iter.hasNext()) {
        key = iter.next();
        val = drawMap.get(key);
        x = Integer.parseInt(key.split(",")[0]) * (int)cellWidth;
        y = Integer.parseInt(key.split(",")[1]) * (int)cellHeight;
        
        int offset = 20;
        for(int i=0; i<val.size(); i++) {
          b.drawString(val.get(i), x + offset, y+(i*15) + offset);
        }
      }
    }
    
    public void drawScreen() {
        Graphics2D g = (Graphics2D) this.getGraphics();
        g.drawImage(buffer, 0, 0, this);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void startGame() {
        Initialize();
        while (true) {
            try {
                drawBuffer();
                drawScreen();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
